package com.kaiashengit.service;

import java.util.List;
import java.util.UUID;

import com.kaisengit.enity.Company;
import com.kaisengit.enity.Guide;

import com.kaisengit.enity.Sales;
import com.kaishengit.dao.CompanyDao;
import com.kaishengit.dao.GuideDao;
import com.kaishengit.dao.ListDao;
import com.kaishengit.util.Pager;



public class SalesService {
	
	private CompanyDao comdao = new CompanyDao();
	private ListDao listdao = new ListDao();
	private GuideDao gudao = new GuideDao();
	
	public void save(Sales sales, String compyname, String name,String tel) {
		Company company = comdao.findByname(compyname);
		if(company == null){
			 company = new Company();
			 UUID uuid = UUID.randomUUID();
			String comid = uuid.toString();
			 company.setId(comid);
			 company.setName(compyname);
			 comdao.save(company);
		}
		
		
		Guide guide = gudao.findByname(name);
		if(guide == null){
			guide = new Guide();
			 UUID uuid = UUID.randomUUID();
			String guid = uuid.toString();
			 guide.setId(guid);
			 guide.setGuidename(name);
			 guide.setTel(tel);
			 gudao.save(guide);
		}
		UUID uuid = UUID.randomUUID();
		String listid = uuid.toString();
		sales.setId(listid);
		sales.setCompany(company);
		sales.setGuide(guide);
		sales.setPrice(sales.getMoney()/sales.getPeoplenum());
		
		listdao.save(sales);
		
		
	}

	public List<Sales> findAll() {
		ListDao listdao = new ListDao();
		return listdao.findAll();
		
		
	}

	public Sales findById(String id) {
		
		return listdao.findyId(id);
	}

	public void edit(Sales sales, String companyname, String guidename,String tel) {

		Company company = comdao.findByname(companyname);
		if(company == null){
			 company = new Company();
			 UUID uuid = UUID.randomUUID();
			 String comid = uuid.toString();
			 company.setId(comid);
			 company.setName(companyname);
			 comdao.save(company);
		}
		GuideDao gudao = new GuideDao();
		
		Guide guide = gudao.findByname(guidename);
		if(guide == null){
			 
			 UUID uuid = UUID.randomUUID();
			 String guid = uuid.toString();
			 guide = new Guide();
			 guide.setId(guid);
			 guide.setGuidename(guidename);
			 guide.setTel(tel);
			 gudao.save(guide);
		}
		
		sales.setCompany(company);
		sales.setGuide(guide);
		sales.setPrice(sales.getMoney() / sales.getPeoplenum());
		
		listdao.update(sales);
		
		
		
	}

	public void del(String id) {
		listdao.del(id);
		
	}

	public Pager<Sales> findByWhere(int pageNum, String companyname,
			String guidename, String address, String starttime,
			String endtime, String minmoney, String maxmoney) {
		
		return listdao.findByWhere(pageNum,companyname,guidename,address,starttime,endtime,minmoney,maxmoney);
	}

	public List<Sales> findByWheres(String companyname, String guidename,
			String address, String starttime, String endtime,
			String minmoney, String maxmoney) {
		
		return listdao.findByWheres(companyname,guidename,address,starttime,endtime,minmoney,maxmoney);
	}

}
